package com.ext.aic.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ext.aic.R;
import com.ext.aic.adapter.GettingCustomersPagerAdapter;
import com.ext.aic.apis.gc.IndustryRequest;
import com.ext.aic.beans.FirstIndustry;
import com.ext.aic.beans.SecondIndustry;
import com.ext.aic.beans.TabItem;
import com.ext.aic.databinding.GettingCustomersLayoutBinding;
import com.ext.aic.enums.SearchSource;
import com.ext.aic.ui.SearchActivity;
import com.ext.aic.utils.RedirectUtils;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GettingCustomersFragment extends BaseFragment {

    private GettingCustomersLayoutBinding binding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.getting_customers_layout, container, false);
        binding = GettingCustomersLayoutBinding.bind(view);
        return binding.getRoot();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        binding.topView.searchAreaRl.setOnClickListener(this::onSearchClick);
        binding.tabStl.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {

            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        industryRequest.request(getContext(), this);
    }

    public void onSearchClick(View view) {
        Bundle bundle = new Bundle();
        bundle.putString("source", SearchSource.CUSTOMERS.name());
        RedirectUtils.getInstance().startActivity(getContext(), SearchActivity.class, bundle);
    }

    private IndustryRequest industryRequest = new IndustryRequest() {
        @Override
        protected void onIndustrySuccess(List<FirstIndustry> industries) {
            GettingCustomersPagerAdapter adapter = new GettingCustomersPagerAdapter(getActivity().getSupportFragmentManager());
            Map<Integer, List<SecondIndustry>> map = new HashMap<>();
            for (int i = 0; i < industries.size(); i++) {
                FirstIndustry industry = industries.get(i);
                adapter.addItem(new TabItem(industry.getName(), industry.getId()));
                map.put(i, industry.getData());
            }
            adapter.setIndustryMap(map);
            binding.listVp.setAdapter(adapter);
            binding.tabStl.setViewPager(binding.listVp);
        }
    };
}
